//
//  LottieRefreshHeader.swift
//  JJToolSwift
//
//  Created by heihei on 2024/6/4.
//

import UIKit
import MJRefresh

class LottieRefreshHeader: MJRefreshHeader {

    var animation: LOTAnimationView = {
        let animation = LOTAnimationView(name: "data")
        animation.frame = CGRect(x: 0, y: 0, width: 64, height: 64)
        animation.centerX = UIDevice.width / 2.00
        animation.loopAnimation = true
        animation.play { finished in
            // 动画完成后需要处理的事情
        }
        return animation
    }()
    
    override func prepare() {
        super.prepare()
        // 自定义刷新控件的属性，比如颜色、文字等
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        // 自定义子视图的布局
        addSubview(animation)
        animation.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(64)
            make.top.equalToSuperview()
        }
    }
    
    override var state: MJRefreshState {
        didSet {
            // 根据state值改变刷新控件的状态和外观
            switch state {
            case .idle:
                // 停止动画，显示初始状态
                animation.stop()
                break
            case .pulling:
                // 松开就可以进行刷新的状态
//                animation.animationProgress = pullingPercent
                break
            case .refreshing:
                // 开始加载数据的动画
                animation.play()
                break
            default:
                break
            }
        }
    }
    
    override var pullingPercent: CGFloat {
        didSet {
            if state == .refreshing {
                return
            }
            if pullingPercent <= 0 {
                animation.stop()
            } else if pullingPercent > 0 && pullingPercent <= 1 {
                animation.animationProgress = pullingPercent
            } else {
                animation.play()
            }
        }
    }

}
